package com.sinaapp.gavinzhang.jianzhioffer;

/**
 * 得到Fibonacci数列的非递归与递归方法
 * Created by gavin on 15-9-3.
 */
public class Fibonacci {
    public static void main(String[] args)
    {
        for (int i = 1; i < 7; i++) {
            System.out.println(getFibonacci(i));
        }
    }

    public static int Fibonacci(int n) {
        if (n==1 || n==2)return 1;
        int n1 = 1;
        int n2 = 1;
        int target = 0;
        for (int i = 2; i < n; i++) {
            target = n1+n2;
            n1 = n2;
            n2 = target;
        }
        return target;

    }

    public static int getFibonacci(int n){
        if (n==1 || n==2)return 1;
        return getFibonacci(n-1)+getFibonacci(n-2);
    }
}
